package com.bw.visitor.view

import android.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.visitor.adapter.NameAdapter
import com.bw.visitor.adapter.VisitorAdapter
import com.bw.visitor.databinding.ActivityVisitorBinding
import com.bw.visitor.entity.VisitorEntity
import com.bw.visitor.module.NameData
import com.bw.visitor.net.VisitorViewModel

/**
 * 智慧园区-来访管理
 * 来访管理
 * 刘哲甫
 * 20250511
 */
@Route(path = RoutePath.PATH_VISITOR)
class VisitorActivity : BaseMvvmActivity<ActivityVisitorBinding,VisitorViewModel>() {
    private lateinit var vAdapter : VisitorAdapter
    private lateinit var nameAdapter: NameAdapter
    private lateinit var dialog: AlertDialog
    private var deletePosition:Int = -1

    private val visitorList : ArrayList<VisitorEntity> = arrayListOf()
    override fun initView() {
        binding.topBar.tvTitle.visibility = View.GONE
        binding.topBar.ivBack.setOnClickListener { finish() }
        binding.topBar.tvLeftTitle.text = "来访管理"
        binding.topBar.tvLeftTitle.visibility = View.VISIBLE
        binding.topBar.llBox.setBackgroundColor(0x1E88E5)

        vAdapter = VisitorAdapter()
        binding.rv.adapter = vAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)

        nameAdapter = NameAdapter()
        nameAdapter.data.clear()
        nameAdapter.data.addAll(NameData.nameList)
        binding.rvFen.adapter = nameAdapter
        binding.rvFen.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        //binding.rv.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)

        nameAdapter.setOnItemClickListener { adapter, view, position ->
            nameAdapter.data.forEach { nameEntity ->
                nameEntity.isShow = false
            }
            nameAdapter.data[position].isShow = true
            nameAdapter.notifyDataSetChanged()
            val filter = visitorList.filter { visitorEntity ->
                visitorEntity.visitStatus.equals(nameAdapter.data[position].name)
            }
            vAdapter.data.clear()


            if (nameAdapter.data[position].name == "全部状态") {
                vAdapter.data.addAll(visitorList)
            }else{
                vAdapter.data.addAll(filter)
            }
            vAdapter.notifyDataSetChanged()

        }

        binding.llAdd.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_ADD_VISITOR)
                .navigation()
        }


        vAdapter.setOnItemLongClickListener { aAdapter, view, position ->
            val entity = vAdapter.data[position]

            dialog = AlertDialog.Builder(this)
                .setNegativeButton(
                    "确定"
                ) { dialog, which ->
                    deletePosition = position
                    vm.deleteVisitor(entity.id)
                    dialog.dismiss()

                }.setPositiveButton("取消") { dialog, which ->
                    dialog.dismiss()

                }
                .setTitle("确定删除吗")
                .create()


            dialog.show()

            true
        }



    }

    override fun initData() {
        vm.visitorSuccess.observe(this){
            vAdapter.data.clear()
            visitorList.clear()
            visitorList.addAll(it.reversed())
            vAdapter.data.addAll(visitorList)
            vAdapter.notifyDataSetChanged()

        }

        vm.deleteVisitorSuccess.observe(this){
            if(deletePosition!=-1){
                vAdapter.notifyItemRemoved(deletePosition)
                vAdapter.data.removeAt(deletePosition)
                ToastUtils.showShort("删除成功")
                deletePosition = -1
            }
        }
    }


    override fun onResume() {
        super.onResume()
        vm.getVisitor()
    }
}